package me.firstapp.weixin.controller.openApi;

import java.math.BigDecimal;
import java.math.RoundingMode;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

import com.foxinmy.weixin4j.exception.WeixinException;
import com.foxinmy.weixin4j.mp.api.TmplApi;
import com.foxinmy.weixin4j.mp.message.TemplateMessage;

import me.firstapp.common.exception.ServiceException;
import me.firstapp.common.exception.StatusHouse;
import me.firstapp.common.json.SingleObject;
import me.firstapp.common.utils.DateUtils;
import me.firstapp.common.utils.HttpUtils;
import me.firstapp.common.utils.ResponseUtils;
import me.firstapp.common.utils.SystemConfig;
import me.firstapp.common.utils.WeiXinConfig;
import me.firstapp.module.weixin.cash.CashApply;
import me.firstapp.module.weixin.jinbi.JinBiAccount;
import me.firstapp.module.weixin.jinbi.JinBiAccountTrace;
import me.firstapp.module.weixin.member.Member;
import me.firstapp.module.weixin.order.ProductOrder;
import me.firstapp.module.weixin.system.AccountRecordType;
import me.firstapp.module.weixin.weibi.WeiBiAccount;
import me.firstapp.module.weixin.weibi.WeiBiAccountTrace;
import me.firstapp.service.weixin.account.JinBiAccountService;
import me.firstapp.service.weixin.account.JinBiAccountTraceService;
import me.firstapp.service.weixin.account.WeiBiAccountService;
import me.firstapp.service.weixin.account.WeiBiAccountTraceService;
import me.firstapp.service.weixin.member.MemberService;
import me.firstapp.service.weixin.product.ProductOrderService;
import me.firstapp.weixin.utils.WchatConfig;

@Controller
@RequestMapping(value = "openApi/messageApi")
public class MessageApi extends BaseApi {

	private static Logger logger = LoggerFactory.getLogger(MessageApi.class);

	@Autowired
	private JinBiAccountService jinBiAccountService;

	@Autowired
	private JinBiAccountTraceService jinBiAccountTraceService;

	@Autowired
	private WeiBiAccountService weiBiAccountService;

	@Autowired
	private WeiBiAccountTraceService weiBiAccountTraceService;

	@Autowired
	private MemberService memberService;

	@Autowired
	private ProductOrderService productOrderService;

	/**
	 * 贝币变动通知
	 * 
	 * @param jinBiAccountTraceId
	 * @param request
	 * @param response
	 */
	@RequestMapping(value = "/sendJinBiChangeMessage.do", method = RequestMethod.POST)
	public void sendJinBiChangeMessage(Long jinBiAccountTraceId, HttpServletRequest request,
			HttpServletResponse response) {
		SingleObject<Object> result = new SingleObject<>();
		try {
			String ip = HttpUtils.getIp2(request);
			logger.info("sendJinBiChangeMessage.do被调用，jinBiAccountTraceId=" + jinBiAccountTraceId + "。请求的IP为：" + ip);
			if (!ACCESS_IP.equals(ip)) {
				throw new ServiceException("error", "非法的请求,ip为：" + ip);
			}
			JinBiAccountTrace jinBiAccountTrace = jinBiAccountTraceService.findById(jinBiAccountTraceId);
			if (null == jinBiAccountTrace) {
				throw new ServiceException("error", "贝币账户流水不存在");
			}
			JinBiAccount jinBiAccount = jinBiAccountService.findById(jinBiAccountTrace.getJinBiAccountId());
			if (null == jinBiAccount) {
				throw new ServiceException("error", "贝币账户不存在");
			}
			Member member = memberService.findMemberByOpenId(jinBiAccount.getOpenId());
			TemplateMessage tplMessage = new TemplateMessage(jinBiAccount.getOpenId(),
					WeiXinConfig.WEIXIN_JINBI_CHANGE_TEMPLATE_ID,
					SystemConfig.mServerUrl + "/page/memberCenterPage.htm");
			String keyword3 = "";
			if (jinBiAccountTrace.getChangeType().getAccountRecordType() == AccountRecordType.ADD) {
				keyword3 = "+" + jinBiAccountTrace.getChangeValue();
			} else if (jinBiAccountTrace.getChangeType().getAccountRecordType() == AccountRecordType.SUB) {
				keyword3 = "-" + jinBiAccountTrace.getChangeValue();
			}
			tplMessage.pushHead("").pushTail("").pushItem("first", member.getName() + "，您好，您的贝币账户余额发生了变动")
					.pushItem("keyword1", DateUtils.getNowStr("yyyy-MM-dd HH:mm:ss"))
					.pushItem("keyword2", jinBiAccountTrace.getChangeType().getDesc()).pushItem("keyword3", keyword3)
					.pushItem("remark", "当前余额为：" + jinBiAccount.getBalance() + "，若需帮助请联系客服。祝您生活愉快！");
			TmplApi tmplApi = new TmplApi(WchatConfig.getInstance().getWeixinProxy().getTokenManager());
			String sendResult = tmplApi.sendTmplMessage(tplMessage);
			System.out.println("消息模板【" + WeiXinConfig.WEIXIN_JINBI_CHANGE_TEMPLATE_ID + "】发送消息返回结果：" + sendResult);
			logger.info("消息模板【" + WeiXinConfig.WEIXIN_JINBI_CHANGE_TEMPLATE_ID + "】发送消息返回结果：" + sendResult);
			result.setStatusObject(StatusHouse.COMMON_STATUS_OK);
			logger.info("sendJinBiChangeMessage.do调用成功");
		} catch (WeixinException e) {
			e.printStackTrace();
			logger.error("sendJinBiChangeMessage.do调用失败", e);
			result.setStatusObject(StatusHouse.COMMON_STATUS_ERROR);
		} catch (ServiceException e) {
			e.printStackTrace();
			e.printStackTrace();
			logger.error("sendJinBiChangeMessage.do调用失败", e);
			result.setStatusObject(e.getStatusObject());
		} catch (Exception e) {
			e.printStackTrace();
			logger.error("sendJinBiChangeMessage.do调用失败", e);
			result.setStatusObject(StatusHouse.COMMON_STATUS_ERROR);
		}
		ResponseUtils.renderJson(response, result);
	}

	/**
	 * 积分变动通知
	 * 
	 * @param jinBiAccountTraceId
	 * @param request
	 * @param response
	 */
	@RequestMapping(value = "/sendWeiBiChangeMessage.do", method = RequestMethod.POST)
	public void sendWeiBiChangeMessage(Long weiBiAccountTraceId, HttpServletRequest request,
			HttpServletResponse response) {
		SingleObject<Object> result = new SingleObject<>();
		try {
			String ip = HttpUtils.getIp2(request);
			logger.info("sendWeiBiChangeMessage.do被调用，weiBiAccountTraceId=" + weiBiAccountTraceId + "。请求的IP为：" + ip);
			if (!ACCESS_IP.equals(ip)) {
				throw new ServiceException("error", "非法的请求,ip为：" + ip);
			}
			WeiBiAccountTrace weiBiAccountTrace = weiBiAccountTraceService.findById(weiBiAccountTraceId);
			if (null == weiBiAccountTrace) {
				throw new ServiceException("error", "积分账户流水不存在");
			}
			WeiBiAccount weiBiAccount = weiBiAccountService.findById(weiBiAccountTrace.getWeiBiAccountId());
			if (null == weiBiAccount) {
				throw new ServiceException("error", "积分账户不存在");
			}
			Member member = memberService.findMemberByOpenId(weiBiAccount.getOpenId());
			TemplateMessage tplMessage = new TemplateMessage(weiBiAccount.getOpenId(),
					WeiXinConfig.WEIXIN_WEIBI_CHANGE_TEMPLATE_ID,
					SystemConfig.mServerUrl + "/page/memberCenterPage.htm");
			String keyword3 = "";
			if (weiBiAccountTrace.getChangeType().getAccountRecordType() == AccountRecordType.ADD) {
				keyword3 = "+" + weiBiAccountTrace.getChangeValue();
			} else if (weiBiAccountTrace.getChangeType().getAccountRecordType() == AccountRecordType.SUB) {
				keyword3 = "-" + weiBiAccountTrace.getChangeValue();
			}
			tplMessage.pushHead("").pushTail("").pushItem("first", member.getName() + "，您好，您的积分账户余额发生了变动")
					.pushItem("keyword1", DateUtils.getNowStr("yyyy-MM-dd HH:mm:ss"))
					.pushItem("keyword2", weiBiAccountTrace.getChangeType().getDesc()).pushItem("keyword3", keyword3)
					.pushItem("remark", "当前余额为：" + weiBiAccount.getBalance() + "，若需帮助请联系客服。祝您生活愉快！");
			TmplApi tmplApi = new TmplApi(WchatConfig.getInstance().getWeixinProxy().getTokenManager());
			String sendResult = tmplApi.sendTmplMessage(tplMessage);
			System.out.println("消息模板【" + WeiXinConfig.WEIXIN_WEIBI_CHANGE_TEMPLATE_ID + "】发送消息返回结果：" + sendResult);
			logger.info("消息模板【" + WeiXinConfig.WEIXIN_WEIBI_CHANGE_TEMPLATE_ID + "】发送消息返回结果：" + sendResult);
			result.setStatusObject(StatusHouse.COMMON_STATUS_OK);
			logger.info("sendWeiBiChangeMessage.do调用成功");
		} catch (WeixinException e) {
			e.printStackTrace();
			logger.error("sendWeiBiChangeMessage.do调用失败", e);
			result.setStatusObject(StatusHouse.COMMON_STATUS_ERROR);
		} catch (ServiceException e) {
			e.printStackTrace();
			e.printStackTrace();
			logger.error("sendWeiBiChangeMessage.do调用失败", e);
			result.setStatusObject(e.getStatusObject());
		} catch (Exception e) {
			e.printStackTrace();
			logger.error("sendWeiBiChangeMessage.do调用失败", e);
			result.setStatusObject(StatusHouse.COMMON_STATUS_ERROR);
		}
		ResponseUtils.renderJson(response, result);
	}

	// @RequestMapping(value = "/sendRechargeSuccessMsg.do", method =
	// RequestMethod.POST)
	// public void sendRechargeSuccessMsg(String openId, String orderNo,
	// HttpServletRequest request,
	// HttpServletResponse response) {
	// SingleObject<Object> result = new SingleObject<>();
	// try {
	// String ip = HttpUtils.getIp2(request);
	// logger.info("sendRechargeSuccessMsg.do被调用，orderNo=" + orderNo +
	// "。请求的IP为：" + ip);
	// if (!ACCESS_IP.equals(ip)) {
	// throw new ServiceException("error", "非法的请求,ip为：" + ip);
	// }
	// MobileFareOrder mobileFareOrder =
	// mobileFareOrderService.findMobileFareOrder(openId, orderNo);
	// if (null == mobileFareOrder) {
	// throw new ServiceException(StatusHouse.STATUS_ORDER_NOT_EXIST);
	// }
	//// Member member =
	// memberService.findMemberByOpenId(mobileFareOrder.getOpenId());
	// TemplateMessage tplMessage = new
	// TemplateMessage(mobileFareOrder.getOpenId(),
	// WeiXinConfig.WEIXIN_HUA_FEI_TEMPLATE_ID, "");
	//// tplMessage.pushHead("").pushTail("").pushItem("first", member.getName()
	// + "，您好，您已成功进行话费充值。")
	//// .pushItem("accountType", "手机号").pushItem("account",
	// mobileFareOrder.getMobile())
	//// .pushItem("amount", mobileFareOrder.getValue() +
	// "元").pushItem("result", "充值成功")
	//// .pushItem("remark", "若需帮助请联系客服。祝您生活愉快！");
	// TmplApi tmplApi = new
	// TmplApi(WchatConfig.getInstance().getWeixinProxy().getTokenManager());
	// String sendResult = tmplApi.sendTmplMessage(tplMessage);
	// System.out.println("消息模板【" + WeiXinConfig.WEIXIN_HUA_FEI_TEMPLATE_ID +
	// "】发送消息返回结果：" + sendResult);
	// logger.info("消息模板【" + WeiXinConfig.WEIXIN_HUA_FEI_TEMPLATE_ID +
	// "】发送消息返回结果：" + sendResult);
	// result.setStatusObject(StatusHouse.COMMON_STATUS_OK);
	// logger.info("sendRechargeSuccessMsg.do调用成功");
	// } catch (WeixinException e) {
	// e.printStackTrace();
	// logger.error("sendRechargeSuccessMsg.do调用失败", e);
	// result.setStatusObject(StatusHouse.COMMON_STATUS_ERROR);
	// } catch (ServiceException e) {
	// e.printStackTrace();
	// e.printStackTrace();
	// logger.error("sendRechargeSuccessMsg.do调用失败", e);
	// result.setStatusObject(e.getStatusObject());
	// } catch (Exception e) {
	// e.printStackTrace();
	// logger.error("sendRechargeSuccessMsg.do调用失败", e);
	// result.setStatusObject(StatusHouse.COMMON_STATUS_ERROR);
	// }
	// ResponseUtils.renderJson(response, result);
	// }

	/**
	 * 等级变更通知
	 * 
	 * @param openId
	 * @param oldLevel
	 * @param newLevel
	 * @param request
	 * @param response
	 */
	@RequestMapping(value = "/sendLevelChangeMsg.do", method = RequestMethod.POST)
	public void sendLevelChangeMsg(String openId, Integer oldLevel, Integer newLevel, HttpServletRequest request,
			HttpServletResponse response) {
		SingleObject<Object> result = new SingleObject<>();
		try {
			String ip = HttpUtils.getIp2(request);
			logger.info("sendLevelChangeMsg.do被调用，openId=" + openId + "oldLevel=" + oldLevel + "newLevel=" + newLevel
					+ "。请求的IP为：" + ip);
			if (!ACCESS_IP.equals(ip)) {
				throw new ServiceException("error", "非法的请求,ip为：" + ip);
			}
			String oldVipStr = getVipName(oldLevel);
			String newVipStr = getVipName(newLevel);
			Member member = memberService.findMemberByOpenId(openId);
			TemplateMessage tplMessage = new TemplateMessage(openId, WeiXinConfig.WEIXIN_LEVEL_CHANGE_TEMPLATE_ID,
					SystemConfig.mServerUrl + "/page/memberCenterPage.htm");

			String keyword3 = "";
			if (Member.VIP_LEVEL_FANS == newLevel) {
				keyword3 = "永久";
			} else {
				keyword3 = DateUtils.getDateStr(member.getExpireDate(), "yyyy-MM-dd HH:mm");
			}
			tplMessage.pushHead("").pushTail("").pushItem("first", member.getName() + "，您的服务已变更！")
					.pushItem("keyword1", oldVipStr).pushItem("keyword2", newVipStr).pushItem("keyword3", keyword3)
					.pushItem("remark", "若需帮助请联系客服。");
			TmplApi tmplApi = new TmplApi(WchatConfig.getInstance().getWeixinProxy().getTokenManager());
			String sendResult = tmplApi.sendTmplMessage(tplMessage);
			System.out.println("消息模板【" + WeiXinConfig.WEIXIN_LEVEL_CHANGE_TEMPLATE_ID + "】发送消息返回结果：" + sendResult);
			logger.info("消息模板【" + WeiXinConfig.WEIXIN_LEVEL_CHANGE_TEMPLATE_ID + "】发送消息返回结果：" + sendResult);
			result.setStatusObject(StatusHouse.COMMON_STATUS_OK);
			logger.info("sendLevelChangeMsg.do调用成功");
		} catch (WeixinException e) {
			e.printStackTrace();
			logger.error("sendLevelChangeMsg.do调用失败", e);
			result.setStatusObject(StatusHouse.COMMON_STATUS_ERROR);
		} catch (ServiceException e) {
			e.printStackTrace();
			e.printStackTrace();
			logger.error("sendLevelChangeMsg.do调用失败", e);
			result.setStatusObject(e.getStatusObject());
		} catch (Exception e) {
			e.printStackTrace();
			logger.error("sendLevelChangeMsg.do调用失败", e);
			result.setStatusObject(StatusHouse.COMMON_STATUS_ERROR);
		}
		ResponseUtils.renderJson(response, result);
	}

	/**
	 * 服务即将到期通知
	 * 
	 * @param openId
	 * @param request
	 * @param response
	 */
	@RequestMapping(value = "/sendLevelExpireMsg.do", method = RequestMethod.POST)
	public void sendLevelExpireMsg(String openId, HttpServletRequest request, HttpServletResponse response) {
		SingleObject<Object> result = new SingleObject<>();
		try {
			String ip = HttpUtils.getIp2(request);
			logger.info("sendLevelChangeMsg.do被调用，openId=" + openId + "。请求的IP为：" + ip);
			if (!ACCESS_IP.equals(ip)) {
				throw new ServiceException("error", "非法的请求,ip为：" + ip);
			}
			Member member = memberService.findMemberByOpenId(openId);
			TemplateMessage tplMessage = new TemplateMessage(openId, WeiXinConfig.WEIXIN_LEVEL_EXPIRE_TEMPLATE_ID,
					SystemConfig.mServerUrl + "/page/memberCenterPage.htm");

			tplMessage.pushHead("").pushTail("")
					.pushItem("first", member.getName() + "，您的" + getVipName(member.getVipLevel()) + "服务即将到期")
					.pushItem("name", getVipName(member.getVipLevel()) + "服务")
					.pushItem("expDate", DateUtils.getDateStr(member.getExpireDate(), "yyyy年MM月dd日"))
					.pushItem("remark", "请及时续费，以防过期。若需帮助请联系客服。");
			TmplApi tmplApi = new TmplApi(WchatConfig.getInstance().getWeixinProxy().getTokenManager());
			String sendResult = tmplApi.sendTmplMessage(tplMessage);
			System.out.println("消息模板【" + WeiXinConfig.WEIXIN_LEVEL_EXPIRE_TEMPLATE_ID + "】发送消息返回结果：" + sendResult);
			logger.info("消息模板【" + WeiXinConfig.WEIXIN_LEVEL_EXPIRE_TEMPLATE_ID + "】发送消息返回结果：" + sendResult);
			result.setStatusObject(StatusHouse.COMMON_STATUS_OK);
			logger.info("sendLevelExpireMsg.do调用成功");
		} catch (WeixinException e) {
			e.printStackTrace();
			logger.error("sendLevelExpireMsg.do调用失败", e);
			result.setStatusObject(StatusHouse.COMMON_STATUS_ERROR);
		} catch (ServiceException e) {
			e.printStackTrace();
			e.printStackTrace();
			logger.error("sendLevelExpireMsg.do调用失败", e);
			result.setStatusObject(e.getStatusObject());
		} catch (Exception e) {
			e.printStackTrace();
			logger.error("sendLevelExpireMsg.do调用失败", e);
			result.setStatusObject(StatusHouse.COMMON_STATUS_ERROR);
		}
		ResponseUtils.renderJson(response, result);
	}

	/**
	 * 发送提现申请审核通知
	 * 
	 * @param openId
	 * @param actualCashAmount
	 * @param applyTime
	 * @param status
	 * @param shTime
	 * @param request
	 * @param response
	 */
	@RequestMapping(value = "/sendCashApplyMsg.do", method = RequestMethod.POST)
	public void sendCashApplyMsg(String openId, String cashNo, BigDecimal actualCashAmount, String applyTime,
			Integer status, String remark, String shTime, HttpServletRequest request, HttpServletResponse response) {
		SingleObject<Object> result = new SingleObject<>();
		try {
			String ip = HttpUtils.getIp2(request);
			logger.info("sendCashApplyMsg.do被调用，openId=" + openId + "。请求的IP为：" + ip);
			if (!ACCESS_IP.equals(ip)) {
				throw new ServiceException("error", "非法的请求,ip为：" + ip);
			}
			TemplateMessage tplMessage = new TemplateMessage(openId, WeiXinConfig.WEIXIN_CASH_RESULT_TEMPLATE_ID,
					SystemConfig.mServerUrl + "/page/cashApplyRecordPage.htm");
			Member member = memberService.findMemberByOpenId(openId);
			String first = member.getName() + ",您好。您的提现申请已处理。";
			String lastRemark = "";
			if (CashApply.STATUS_WTG == status) {
				String yy = "原因：" + remark;
				lastRemark = yy + " 有任何疑问，请联系客服。";
			} else if (status == CashApply.STATUS_SH) {
				lastRemark = "【重要】请及时联系客服，提供您的ID（" + member.getCardNo() + "）和提现申请编号（" + cashNo
						+ "），以便客服快速审核通过。提现客服微信号：1046566144";
			} else {
				lastRemark = "有任何疑问，请联系客服。";
			}
			tplMessage.pushHead("").pushTail("").pushItem("first", first)
					.pushItem("keyword1", "￥" + actualCashAmount.setScale(2, RoundingMode.HALF_UP).doubleValue())
					.pushItem("keyword2", "微信").pushItem("keyword3", applyTime)
					.pushItem("keyword4", CashApply.statusMap.get(status)).pushItem("keyword5", shTime)
					.pushItem("remark", lastRemark);
			TmplApi tmplApi = new TmplApi(WchatConfig.getInstance().getWeixinProxy().getTokenManager());
			String sendResult = tmplApi.sendTmplMessage(tplMessage);
			System.out.println("消息模板【" + WeiXinConfig.WEIXIN_CASH_RESULT_TEMPLATE_ID + "】发送消息返回结果：" + sendResult);
			logger.info("消息模板【" + WeiXinConfig.WEIXIN_CASH_RESULT_TEMPLATE_ID + "】发送消息返回结果：" + sendResult);
			logger.info("sendCashApplyMsg.do调用成功");
		} catch (WeixinException e) {
			e.printStackTrace();
			logger.error("sendCashApplyMsg.do调用失败", e);
			result.setStatusObject(StatusHouse.COMMON_STATUS_ERROR);
		} catch (ServiceException e) {
			e.printStackTrace();
			e.printStackTrace();
			logger.error("sendCashApplyMsg.do调用失败", e);
			result.setStatusObject(e.getStatusObject());
		} catch (Exception e) {
			e.printStackTrace();
			logger.error("sendCashApplyMsg.do调用失败", e);
			result.setStatusObject(StatusHouse.COMMON_STATUS_ERROR);
		}
		ResponseUtils.renderJson(response, result);
	}

	/**
	 * 发货通知
	 * 
	 * @param orderNo
	 * @param request
	 * @param response
	 */
	@RequestMapping(value = "/sendOrderMsg.do", method = RequestMethod.POST)
	public void sendOrderMsg(String orderNo, HttpServletRequest request, HttpServletResponse response) {
		SingleObject<Object> result = new SingleObject<>();
		try {
			String ip = HttpUtils.getIp2(request);
			logger.info("sendOrderMsg.do被调用。请求的IP为：" + ip);
			if (!ACCESS_IP.equals(ip)) {
				throw new ServiceException("error", "非法的请求,ip为：" + ip);
			}
			ProductOrder productOrder = productOrderService.findByOrderNo(orderNo);
			Member member = memberService.findMemberByOpenId(productOrder.getOpenId());
			TemplateMessage tplMessage = new TemplateMessage(productOrder.getOpenId(),
					WeiXinConfig.WEIXIN_ORDER_SEND_TEMPLATE_ID,
					SystemConfig.mServerUrl + "/page/productOrderDetailView.htm?orderNo=" + orderNo);
			tplMessage.pushHead("").pushTail("")
					.pushItem("first", member.getName() + "，您购买的【" + productOrder.getName() + "】已经发货啦，正快马加鞭向您飞奔而去。")
					.pushItem("keyword1", productOrder.getName())
					.pushItem("keyword2", DateUtils.getNowStr("yyyy-MM-dd HH:mm"))
					.pushItem("keyword3", productOrder.getFreightName())
					.pushItem("keyword4", productOrder.getFreightNo())
					.pushItem("keyword5", productOrder.getReceiveAddress()).pushItem("remark", "更多超值商品值得选购哦，点击详情查看！");
			TmplApi tmplApi = new TmplApi(WchatConfig.getInstance().getWeixinProxy().getTokenManager());
			String sendResult = tmplApi.sendTmplMessage(tplMessage);
			System.out.println("消息模板【" + WeiXinConfig.WEIXIN_JINBI_CHANGE_TEMPLATE_ID + "】发送消息返回结果：" + sendResult);
			logger.info("消息模板【" + WeiXinConfig.WEIXIN_JINBI_CHANGE_TEMPLATE_ID + "】发送消息返回结果：" + sendResult);
			logger.info("sendOrderMsg.do调用成功");
		} catch (WeixinException e) {
			e.printStackTrace();
			logger.error("sendOrderMsg.do调用失败", e);
			result.setStatusObject(StatusHouse.COMMON_STATUS_ERROR);
		} catch (ServiceException e) {
			e.printStackTrace();
			e.printStackTrace();
			logger.error("sendOrderMsg.do调用失败", e);
			result.setStatusObject(e.getStatusObject());
		} catch (Exception e) {
			e.printStackTrace();
			logger.error("sendOrderMsg.do调用失败", e);
			result.setStatusObject(StatusHouse.COMMON_STATUS_ERROR);
		}
		ResponseUtils.renderJson(response, result);
	}

	/**
	 * 退货通知
	 * 
	 * @param orderNo
	 * @param request
	 * @param response
	 */
	@RequestMapping(value = "/returnOrderMsg.do", method = RequestMethod.POST)
	public void returnOrderMsg(String orderNo, HttpServletRequest request, HttpServletResponse response) {
		SingleObject<Object> result = new SingleObject<>();
		try {
			String ip = HttpUtils.getIp2(request);
			logger.info("returnOrderMsg.do被调用。请求的IP为：" + ip);
			if (!ACCESS_IP.equals(ip)) {
				throw new ServiceException("error", "非法的请求,ip为：" + ip);
			}
			ProductOrder productOrder = productOrderService.findByOrderNo(orderNo);
			Member member = memberService.findMemberByOpenId(productOrder.getOpenId());
			TemplateMessage tplMessage = new TemplateMessage(productOrder.getOpenId(),
					WeiXinConfig.WEIXIN_ORDER_RETURN_TEMPLATE_ID,
					SystemConfig.mServerUrl + "/page/productOrderDetailView.htm?orderNo=" + orderNo);
			tplMessage.pushHead("").pushTail("")
					.pushItem("first", member.getName() + "，您购买的【" + productOrder.getName() + "】已经退货。")
					.pushItem("keyword1", productOrder.getOrderNo()).pushItem("keyword2", productOrder.getName())
					.pushItem("keyword3", productOrder.getCounts() + "")
					.pushItem("keyword4",
							productOrder.getPayAmount().setScale(2, RoundingMode.HALF_UP).toString() + "贝币")
					.pushItem("remark", "若需帮助请联系客服，欢迎再次光临！");
			TmplApi tmplApi = new TmplApi(WchatConfig.getInstance().getWeixinProxy().getTokenManager());
			String sendResult = tmplApi.sendTmplMessage(tplMessage);
			System.out.println("消息模板【" + WeiXinConfig.WEIXIN_ORDER_RETURN_TEMPLATE_ID + "】发送消息返回结果：" + sendResult);
			logger.info("消息模板【" + WeiXinConfig.WEIXIN_ORDER_RETURN_TEMPLATE_ID + "】发送消息返回结果：" + sendResult);
			logger.info("returnOrderMsg.do调用成功");
		} catch (WeixinException e) {
			e.printStackTrace();
			logger.error("returnOrderMsg.do调用失败", e);
			result.setStatusObject(StatusHouse.COMMON_STATUS_ERROR);
		} catch (ServiceException e) {
			e.printStackTrace();
			e.printStackTrace();
			logger.error("returnOrderMsg.do调用失败", e);
			result.setStatusObject(e.getStatusObject());
		} catch (Exception e) {
			e.printStackTrace();
			logger.error("returnOrderMsg.do调用失败", e);
			result.setStatusObject(StatusHouse.COMMON_STATUS_ERROR);
		}
		ResponseUtils.renderJson(response, result);
	}
}
